/**
 * Copyright &copy; 2016-2017 <a href="http://git.oschina.net/whoamien/backend_management">Backend Management</a> All rights reserved.
 */
package com.xw.app.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xw.app.model.AcctRole;
import com.xw.app.model.SysMenu;
import com.xw.app.service.SysMenuService;
import com.xw.app.vo.MenuSelection;
import com.xw.framework.base.controller.BaseSpringController;
import com.xw.framework.vo.acetree.AceTreeItem;
import com.xw.framework.vo.acetree.AdditionalParameters;

/**
 * The Class SysMenuController.
 *
 * @author Xi Wei
 */
@Controller
@RequestMapping("/sysMenu")
public class SysMenuController extends BaseSpringController {

	@Autowired
	private SysMenuService sysMenuService;
	
	/**
	 * List.
	 *
	 * @return the model and view
	 */
	@RequestMapping("/list")
	public ModelAndView list(){
		ModelAndView result = new ModelAndView("/SysMenu/list");
		ModelMap map = new ModelMap();
		List<SysMenu> menuList = sysMenuService.findByParentIsNull();
		map.addAttribute("menuList", menuList);
		result.addAllObjects(map);
		return result;
	}

	
	/**
	 * To assign role.
	 *
	 * @return the model and view
	 */
	@RequestMapping("/toAssignRole")
	public ModelAndView toAssignRole()
	{
		ModelAndView result = new ModelAndView("/SysMenu/roleMenuZTree");
		ModelMap map = new ModelMap();
		result.addAllObjects(map);
		return result;
	}
	
	/**
	 * To assign menu.
	 *
	 * @param acctRole the acct role
	 * @return the model and view
	 */
	@RequestMapping("/popup/toAssignMenu")
	public ModelAndView toAssignMenu(AcctRole acctRole)
	{
		ModelAndView result = new ModelAndView("/SysMenu/assignMenuForRole");
		ModelMap map = new ModelMap();
		map.addAttribute("acctRole", acctRole);
		result.addAllObjects(map);
		return result;
	}
	
	/**
	 * Update menu for role.
	 *
	 * @param request the request
	 * @return the model and view
	 */
	@RequestMapping("/updateMenuForRole")
	public ModelAndView updateMenuForRole(HttpServletRequest request){
		Long roleId = Long.valueOf(request.getParameter("roleId"));
		String checkedMenu = request.getParameter("checkedMenu");
		sysMenuService.updateMenuForRole(checkedMenu, roleId);
		ModelMap map = new ModelMap();
		ModelAndView result = new ModelAndView("/saveResult");
		map.addAttribute("msg", "success");
		
		result.addAllObjects(map);
		return result;
	}

	/**
	 * Gets the menu all.
	 *
	 * @param id the id
	 * @return the menu all
	 */
	@RequestMapping(value={"/getMenuAll","/json/getMenuAll"})
	@ResponseBody
	public List<MenuSelection> getMenuAll(Long id){
		return sysMenuService.GetAllMenuForAssignment(id);
	}
	
	/**
	 * Gets the menu tree.
	 *
	 * @return the menu tree
	 */
	@RequestMapping(value={"/getMenuTree","/json/getMenuTree"})
	@ResponseBody
	public Map<String,AceTreeItem> getMenuTree(){
		
		Map<String,AceTreeItem> aceTreeItemMap = new HashMap<String,AceTreeItem>();
		AceTreeItem aceTreeItem = new AceTreeItem();
		aceTreeItem.setName("For Sale");
		aceTreeItem.setType("folder");
		aceTreeItemMap.put("For Sale", aceTreeItem);
		
		AdditionalParameters additionalParameters = new AdditionalParameters();
		additionalParameters.setId(1l);
		
		aceTreeItem.setAdditionalParameters(additionalParameters);
		
		aceTreeItem = new AceTreeItem();
		aceTreeItem.setName("Appliances");
		aceTreeItem.setType("item");
		additionalParameters.addAceTreeItem(aceTreeItem);
		
		aceTreeItem = new AceTreeItem();
		aceTreeItem.setName("Clothing");
		aceTreeItem.setType("item");
		additionalParameters.addAceTreeItem(aceTreeItem);
		
		aceTreeItem = new AceTreeItem();
		aceTreeItem.setName("Sports");
		aceTreeItem.setType("item");
		additionalParameters.addAceTreeItem(aceTreeItem);
		
		
		
		
		aceTreeItem = new AceTreeItem();
		aceTreeItem.setName("Vehicles");
		aceTreeItem.setType("folder");
		aceTreeItemMap.put("Vehicle", aceTreeItem);
		
		additionalParameters = new AdditionalParameters();
		additionalParameters.setId(2l);
		aceTreeItem.setAdditionalParameters(additionalParameters);
		
		AceTreeItem carItem = new AceTreeItem();
		carItem.setName("Cars");
		carItem.setType("folder");		
		additionalParameters.addAceTreeItem(carItem);
		
		
		additionalParameters = new AdditionalParameters();
		additionalParameters.setId(3l);
		carItem.setAdditionalParameters(additionalParameters);
		
		aceTreeItem = new AceTreeItem();
		aceTreeItem.setName("Classic");
		aceTreeItem.setType("item");
		additionalParameters.addAceTreeItem(aceTreeItem);
		
		aceTreeItem = new AceTreeItem();
		aceTreeItem.setName("Coupes");
		aceTreeItem.setType("item");
		additionalParameters.addAceTreeItem(aceTreeItem);
		
		return aceTreeItemMap;
		
		
		
		
	}
	
}
